package com.ruoyi.dish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dish.domain.Dish;
import com.ruoyi.dish.domain.DishFlavor;
import com.ruoyi.dish.mapper.DishMapper;
import com.ruoyi.dish.mapper.mp.MpDishFlavorMapper;
import com.ruoyi.dish.mapper.mp.MpDishMapper;
import com.ruoyi.dish.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 菜品管理Service业务层处理
 * 
 * @author yutao
 * @date 2024-08-04
 */
@Service
public class DishServiceImpl implements IDishService 
{
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private MpDishMapper mpDishMapper;;

    @Autowired
    private MpDishFlavorMapper mpDishFlavorMapper;

    /**
     * 查询菜品管理
     * 
     * @param id 菜品管理主键
     * @return 菜品管理
     */
    @Override
    public Dish selectDishById(Long id)
    {
        return dishMapper.selectDishById(id);
    }

    /**
     * 查询菜品管理列表
     * 
     * @param dish 菜品管理
     * @return 菜品管理
     */
    @Override
    public List<Dish> selectDishList(Dish dish)
    {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<Dish>();
        queryWrapper.lambda().eq(Objects.nonNull(dish.getStatus()), Dish::getStatus,dish.getStatus())
                .like(StringUtils.isNotEmpty(dish.getName()), Dish::getName,dish.getName());
        return mpDishMapper.selectList(queryWrapper);
//        return dishMapper.selectDishList(dish);
    }

    /**
     * 新增菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDish(Dish dish)
    {
        dish.setCreateTime(DateUtils.getNowDate());
        int rows = mpDishMapper.insert(dish);
//        int rows = dishMapper.insertDish(dish);
//        insertDishFlavor(dish);
        return rows;
    }

    /**
     * 修改菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDish(Dish dish)
    {
        dish.setUpdateTime(DateUtils.getNowDate());
//        dishMapper.deleteDishFlavorByDishId(dish.getId());
//        insertDishFlavor(dish);
        mpDishFlavorMapper.delete(new QueryWrapper<DishFlavor>().lambda().eq(DishFlavor::getDishId,dish.getId()));
        List<DishFlavor> dishFlavorList = dish.getDishFlavorList();
        dishFlavorList.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
            mpDishFlavorMapper.insert(dishFlavor);
        });
//
        return dishFlavorList.size();
    }

    /**
     * 批量删除菜品管理
     * 
     * @param ids 需要删除的菜品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishByIds(Long[] ids)
    {
//        dishMapper.deleteDishFlavorByDishIds(ids);
//        return dishMapper.deleteDishByIds(ids);
        mpDishFlavorMapper.delete(new QueryWrapper<DishFlavor>().lambda().in(DishFlavor::getDishId,ids));
        mpDishMapper.delete(new QueryWrapper<Dish>().lambda().in(Dish::getId,ids));
        return ids.length;
    }

    /**
     * 删除菜品管理信息
     * 
     * @param id 菜品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishById(Long id)
    {
//        dishMapper.deleteDishFlavorByDishId(id);
//        return dishMapper.deleteDishById(id);
        dishMapper.deleteDishById( id);
        mpDishFlavorMapper.delete(new QueryWrapper<DishFlavor>().lambda().eq(DishFlavor::getDishId,id));
        return 1;
    }

    /**
     * 新增菜品口味关系信息
     * 
     * @param dish 菜品管理对象
     */
    public void insertDishFlavor(Dish dish)
    {
        dishMapper.updateDish(dish);
        dish.getDishFlavorList().forEach(
                dishFlavor -> {
                    dishFlavor.setDishId(dish.getId());
                    mpDishFlavorMapper.insert(dishFlavor);
                }
        );
//
//        Long id = dish.getId();
//        if (StringUtils.isNotNull(dishFlavorList))
//        {
//            List<DishFlavor> list = new ArrayList<DishFlavor>();
//            for (DishFlavor dishFlavor : dishFlavorList)
//            {
//                dishFlavor.setDishId(id);
//                list.add(dishFlavor);
//            }
//            if (list.size() > 0)
//            {
//                dishMapper.batchDishFlavor(list);
//            }
//        }
    }
}
